﻿using Sunny.UI;

namespace supermarket.page
{
    public partial class querygoods : UIPage
    {
        public querygoods()
        {
            InitializeComponent();
        }

        private void querygoods_Load(object sender, EventArgs e)
        {
            res res = HTTPUtils.GET("goods/all");
            uiDataGridView1.DataSource = res.date;
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            String q = textBoxEx1.Text;
            if (q == "")
            {
                querygoods_Load(sender, e);
                return;
            }
            res res = HTTPUtils.jsonPost("goods/query", new { query = q, });
            if (res.cod != "200")
            {
                MessageBox.Show(res.cod, "错误", MessageBoxButtons.OK);
                return;
            }
            uiDataGridView1.DataSource = res.date;
        }

        private void querygoods_Initialize(object sender, EventArgs e)
        {
            uiDataGridView1.ReadOnly = true;
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            querygoods_Load(sender, e);
        }

        private void uiDataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            UIEditForm frm = form.goodsSaleForm(uiDataGridView1.CurrentCell.OwningRow.Cells[1].Value.ToString()+"的出售");
            frm.Render();
            frm.ShowDialog();
            if (frm.IsOK)
            {
                res res=HTTPUtils.jsonPost("goods/sale", new
                {
                    goodsid = uiDataGridView1.CurrentCell.OwningRow.Cells[0].Value.ToString(),
                    quantity = frm["mod"],
                    userid = Formmain.user.id
                });
                if (res.cod != "200")
                {

                    MessageBox.Show(res.msg, "出售该商品失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    querygoods_Load(sender, e);
                    MessageBox.Show(res.msg, "出售该商品成功！", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void uiDataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                String re = MessageBox.Show("是否真的删除！", "错误提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information).ToString();
                if (re == "Yes")
                {
                    res res = HTTPUtils.jsonPost("goods/dal", new
                    {
                        id = uiDataGridView1.CurrentCell.OwningRow.Cells[0].Value.ToString()
                    });
                    if (res.cod != "200")
                    {

                        MessageBox.Show(res.msg, "删除该商品失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else
                    {
                        querygoods_Load(sender, e);
                        MessageBox.Show(res.msg, "删除该商品成功！", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            UIEditForm frm = form.goodscancelForm(uiDataGridView1.CurrentCell.OwningRow.Cells[1].Value.ToString() + "的退货");
            frm.Render();
            frm.ShowDialog();
            if (frm.IsOK)
            {
                res res = HTTPUtils.jsonPost("goods/cancel", new
                {
                    goodsid = uiDataGridView1.CurrentCell.OwningRow.Cells[0].Value.ToString(),
                    quantity = frm["quantity"],
                    userid = Formmain.user.id,
                    reason = frm["reason"]
                });
                if (res.cod != "200")
                {

                    MessageBox.Show(res.msg, "该商品退货失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    querygoods_Load(sender, e);
                    MessageBox.Show(res.msg, "该商品退货成功！", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }
}
            

